package cn.daniellee.plugin.pm.component;

import cn.daniellee.plugin.pm.PartnerMission;
import cn.daniellee.plugin.pm.core.MissionCore;
import cn.daniellee.plugin.pm.model.Mission;
import cn.daniellee.plugin.pm.model.PlayerData;
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * PlaceholderAPI扩展
 */
public class PlaceholderHook extends PlaceholderExpansion {


    private final PartnerMission plugin;

    public PlaceholderHook(PartnerMission plugin){
        this.plugin = plugin;
    }

    @Override
    public boolean persist(){
        return true;
    }

    @Override
    public boolean canRegister(){
        return true;
    }

    @Override
    public @NotNull String getAuthor(){
        return plugin.getDescription().getAuthors().toString();
    }

    @Override
    public @NotNull String getIdentifier(){
        return "pm";
    }

    @Override
    public @NotNull String getVersion(){
        return plugin.getDescription().getVersion();
    }

    @Override
    public String onPlaceholderRequest(Player player, @NotNull String identifier){
        if (player == null) {
            if (identifier.startsWith("complete_")) {
                int rank = Integer.parseInt(identifier.replace("complete_", ""));
                List<PlayerData> allPlayer = MissionCore.getAndSortAllPlayers(true);
                if (rank < allPlayer.size()) {
                    PlayerData playerData = allPlayer.get(rank);
                    return PartnerMission.getInstance().getConfig().getString("papi.global.complete.rank", "&b{rank}: &e{player} &aComplete {times} Times").replace("{rank}", Integer.toString(rank)).replace("{player}", playerData.getName()).replace("{times}", Integer.toString(playerData.getFinishTimes())).replace("&", "§");
                }
                return PartnerMission.getInstance().getConfig().getString("papi.global.complete.none", "&b{rank}: &cNobody").replace("{rank}", Integer.toString(rank)).replace("&", "§");
            } else if (identifier.startsWith("online_")) {
                int rank = Integer.parseInt(identifier.replace("online_", ""));
                List<PlayerData> allPlayer = MissionCore.getAndSortAllPlayers(false);
                if (rank < allPlayer.size()) {
                    PlayerData playerData = allPlayer.get(rank);
                    return PartnerMission.getInstance().getConfig().getString("papi.global.online.rank", "&b{rank}: &e{player} &d{days} Days {hours} Hours").replace("{rank}", Integer.toString(rank)).replace("{player}", playerData.getName()).replace("{days}", Integer.toString(playerData.getOnlineMinutes() / 1440)).replace("{hours}", Integer.toString((playerData.getOnlineMinutes() % 1440) / 60)).replace("&", "§");
                }
                return PartnerMission.getInstance().getConfig().getString("papi.global.online.none", "&b{rank}: &cNobody").replace("{rank}", Integer.toString(rank)).replace("&", "§");
            }
        } else {
            PlayerData playerData = PartnerMission.getInstance().getStorage().getPlayerByName(player.getName());
            switch (identifier) {
                case "status" -> {
                    Mission mission = MissionCore.getPlayerActiveMission(playerData.getName());
                    long before = System.currentTimeMillis() - PartnerMission.getInstance().getConfig().getLong("setting.cooldown-period", 60) * 60 * 1000;
                    if (playerData.getLastFinish() > before) return PartnerMission.getInstance().getConfig().getString("papi.player.status.cooldown", "&dCooldown: {cooldown} Minutes").replace("{cooldown}", Long.toString((playerData.getLastFinish() - before) / 1000 / 60)).replace("&", "§");
                    if (mission == null) return PartnerMission.getInstance().getConfig().getString("papi.player.status.none", "&bIdle").replace("&", "§");
                    return PartnerMission.getInstance().getConfig().getString("papi.player.status.progress", "&6Remaining: {remain} Minutes").replace("{remain}", Long.toString((mission.getValidUntil() - System.currentTimeMillis()) / 1000 / 60)).replace("&", "§");
                }
                case "target" -> {
                    Mission mission = MissionCore.getPlayerActiveMission(playerData.getName());
                    if (mission == null) return PartnerMission.getInstance().getConfig().getString("papi.player.target.none", "&cNo Mission").replace("&", "§");
                    if (mission.getProgress() == 5) return PartnerMission.getInstance().getConfig().getString("papi.player.target.complete", "&6Completed").replace("&", "§");
                    int current = 0, target = 0;
                    switch (mission.getProgress()) {
                        case 0 -> {
                            current = mission.getFellingCurrent();
                            target = MissionCore.felling.get(mission.getFellingIndex()).getTarget();
                        }
                        case 1 -> {
                            current = mission.getMiningCurrent();
                            target = MissionCore.mining.get(mission.getMiningIndex()).getTarget();
                        }
                        case 2 -> {
                            current = mission.getKillMonsterCurrent();
                            target = MissionCore.killMonster.get(mission.getKillMonsterIndex()).getTarget();
                        }
                        case 3 -> {
                            current = mission.getKillAnimalCurrent();
                            target = MissionCore.killAnimal.get(mission.getKillAnimalIndex()).getTarget();
                        }
                        case 4 -> {
                            current = mission.getGatherCropCurrent();
                            target = MissionCore.gatherCrop.get(mission.getGatherCropIndex()).getTarget();
                        }
                    }
                    return PartnerMission.getInstance().getConfig().getString("papi.player.target.progress", "&dProgress: &e{current}/{target}").replace("{current}", Integer.toString(current)).replace("{target}", Integer.toString(target)).replace("&", "§");
                }
                case "partner" -> {
                    Mission mission = MissionCore.getPlayerActiveMission(playerData.getName());
                    if (mission == null) return PartnerMission.getInstance().getConfig().getString("papi.player.partner.none", "&cNo Partner").replace("&", "§");
                    String partner = playerData.getName().equals(mission.getInviter()) ? mission.getInvitee() : mission.getInviter();
                    return PartnerMission.getInstance().getConfig().getString("papi.player.partner.player", "&ePartner: {player}").replace("{player}", partner).replace("&", "§");
                }
                case "complete" -> {
                    return PartnerMission.getInstance().getConfig().getString("papi.player.complete", "&a{times} Times").replace("{times}", Integer.toString(playerData.getFinishTimes())).replace("&", "§");
                }
                case "online" -> {
                    return PartnerMission.getInstance().getConfig().getString("papi.player.online", "&d{days} Days {hours} Hours").replace("{days}", Integer.toString(playerData.getOnlineMinutes() / 1440)).replace("{hours}", Integer.toString((playerData.getOnlineMinutes() % 1440) / 60)).replace("&", "§");
                }
            }
        }
        return null;
    }
}
